package com.ht.medicaltreatment.view

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.BitmapDrawable
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager.LayoutParams
import android.widget.PopupWindow
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ScreenUtils
import com.blankj.utilcode.util.ToastUtils
import com.gym.baselib.base.BaseFragment
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.fragment.ItemFragment
import com.ht.medicaltreatment.utils.backgroundAlpha

class SampleBoxLocationPop(context:Context):PopupWindow(context), View.OnClickListener {
    private val context = context
    private val view:View
    private var tvFridge:TextView
    private var tvFloor:TextView
    private var tvDrawer:TextView
    private var tvLocation:TextView
    private var viewPager:ViewPager2
    private var fragments:MutableList<BaseFragment> = mutableListOf()
    private var onClick:(String,Int) -> Unit = {name,id ->}
    private var currentPosition = 0
    //冰箱，层数，抽屉，位置
    var ids = arrayOf(-1,-1,-1,-1)
    var textViews = arrayOfNulls<TextView>(4)
    var names = arrayOf("","","","")
    var titles = arrayOf("冰箱","层数","抽屉","位置")

    init {
        view =
            LayoutInflater.from(context).inflate(R.layout.pop_choose_location_layout, null, false)
        contentView = view
        isOutsideTouchable = true
        setBackgroundDrawable(BitmapDrawable())
        isTouchable = true
        width = LayoutParams.MATCH_PARENT
        height = ScreenUtils.getScreenHeight()/2
        animationStyle = R.style.itemPopStyle

        viewPager = view.findViewById(R.id.viewPager)
        tvFloor = view.findViewById(R.id.tvFloor)
        tvDrawer = view.findViewById(R.id.tvDrawer)
        tvLocation = view.findViewById(R.id.tvLocation)
        tvFridge = view.findViewById(R.id.tvFridge)

        textViews[0] = tvFridge
        textViews[1] = tvFloor
        textViews[2] = tvDrawer
        textViews[3] = tvLocation

        initClick()

        setOnDismissListener {
            ActivityUtils.getTopActivity().backgroundAlpha(1.0f)
        }
    }


    private fun initClick() {
        tvFridge.setOnClickListener(this)
        tvFloor.setOnClickListener(this)
        tvDrawer.setOnClickListener(this)
        tvLocation.setOnClickListener(this)
    }

    fun initTab(tabTitle:MutableList<String>,fragments:MutableList<BaseFragment>) {
        this.fragments.clear()
        this.fragments.addAll(fragments)
        viewPager.offscreenPageLimit = 1
        viewPager.isUserInputEnabled = false
        if(context is Fragment){
            viewPager.adapter = object : FragmentStateAdapter(context){
                override fun getItemCount() = tabTitle.size

                override fun createFragment(position: Int) = fragments[position]
            }
        }else if(context is AppCompatActivity){
            viewPager.adapter = object : FragmentStateAdapter(context){
                override fun getItemCount() = tabTitle.size

                override fun createFragment(position: Int) = fragments[position]
            }
        }
    }

    fun show(onClick:(String,Int)->Unit){
        if(isShowing){
            return
        }
        this.onClick = onClick
        ActivityUtils.getTopActivity().backgroundAlpha(0.4f)
        showAtLocation(view,Gravity.BOTTOM,0,0)

        next()
    }

    override fun onClick(v: View?) {
        when(v){
            tvFridge -> {
                viewPager.setCurrentItem(0,false)
                currentPosition = 0
                next()
            }
            tvFloor -> {
                if(ids[0] == -1){
                    ToastUtils.showShort("请先选择冰箱")
                    return
                }
                viewPager.setCurrentItem(1,false)
                currentPosition = 1
                next()
            }
            tvDrawer -> {
                if(ids[1] == -1){
                    ToastUtils.showShort("请先选择层数")
                    return
                }
                viewPager.setCurrentItem(2,false)
                currentPosition = 2
                next()
            }
            tvLocation -> {
                if(ids[2] == -1){
                    ToastUtils.showShort("请先选择抽屉")
                    return
                }
                viewPager.setCurrentItem(3,false)
                currentPosition = 3
                next()
            }
        }
    }

    private fun next(){
        if(currentPosition <= 3){
            LogUtils.i("next: $currentPosition")
            var preId = -1
            if(currentPosition > 0){
                preId = ids[currentPosition-1]
            }
            (fragments[currentPosition] as ItemFragment).initData(currentPosition,ids[currentPosition],preId) { name, id ->
                textViews[currentPosition]!!.text = "${titles[currentPosition]}: $name"
                textViews[currentPosition]!!.setTextColor(Color.BLUE)
                ids[currentPosition] = id
                names[currentPosition] = name
                LogUtils.i("${titles[currentPosition]} 选择了 $name")
                //点击之后，后面的要重新开始选择
                for (i in currentPosition+1 until 4){
                    LogUtils.i("恢复  $i")
                    ids[i] = -1
                    textViews[i]?.text = textViews[i]?.tag.toString()
                    textViews[i]!!.setTextColor(Color.BLACK)
                }

                if(currentPosition < 3){
                    currentPosition++
                    viewPager.setCurrentItem(currentPosition)
                    next()
                }else{
                    var n = ""
                    names.forEach {
                        n = "$n-$it"
                    }
                    onClick(n.substring(1,n.length),id)
                    dismiss()
                }
            }
        }
    }
}